from django.db.models import Q
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet, GenericViewSet

from app_service.models.school_model import Student
from app_service.serializers.school_serializer import StudentSerializer
from app_service.responses import default_response

import logging

logger = logging.getLogger(__name__)


class StudentAPIView(APIView):
    authentication_classes = [
        # BasicAuthentication,
        SessionAuthentication,
    ]
    permission_classes = [IsAuthenticated, ]

    def get(self, request, ):
        logger.debug('xxx')
        return Response(default_response())


def StudentViewSetMixin(ViewSetMixin):
    def get(self, request, ):
        logger.debug('xxx')
        return Response(default_response())


class StudentGenericViewSet(GenericViewSet):
    queryset = Student.objects.all().filter(Q(is_delete__exact=0))
    serializer_class = StudentSerializer

    lookup_field = 'pk'

    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return Response(data=serializer.data)

    @action(methods=['post'], detail=True, url_path='custom_action')
    def custom_method(self, request, pk):
        logger.info('request.body:' + str(request.body))
        # student: Student = self.get_object()
        student: Student = self.queryset.get(pk=pk)
        logger.info('student.name:%s', student.name)
        serializer = self.get_serializer(instance=student)
        return Response(data=serializer.data,
                        status=status.HTTP_200_OK)

    pass


class StudentModelViewSet(ModelViewSet):
    queryset = Student.objects.all().filter(Q(is_delete__exact=1))
    serializer_class = StudentSerializer

    # permission_classes = [IsAuthenticated]

    pass
